package jam.UserInterface.UI;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.colorchooser.*;
import javax.swing.text.*;
import java.io.File;
import javax.imageio.ImageIO;

import java.awt.*;
import java.awt.event.*;

    //some of this code was taken from the book Java Swing 2nd edition by Marc Loy, Robert Eckstein, Dave Wood, James Elliott and Brian Cole, published in 2002 by O'Reilly, USA.

public class fontChooserWindow extends JDialog implements ActionListener {

	private JColorChooser colorChooser;
	private JComboBox fontName;
	private JCheckBox fontBold, fontItalic;
	private JTextField fontSize;
	private JLabel previewLabel;
	private SimpleAttributeSet attributes;
	private Font newFont;
	private Color newColor;
	private JEditorPane messageBox;
	
	public fontChooserWindow(Frame parent,JEditorPane messageBox){
		super(parent, "Font Chooser", true);
		this.messageBox = messageBox;
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		setBounds(((int)(screenSize.getWidth()-450))/2,((int)(screenSize.getHeight()-450))/2,450,450);
		attributes = new SimpleAttributeSet();
		
		addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				closeAndCancel();
			}
		});
		Container c = getContentPane();
		
		//change the icon code
		String currentDirectory = System.getProperty("user.dir");
		try {
			Image image = ImageIO.read(new File(currentDirectory + "/src/jam/UserInterface/Icons/JAMicon.png"));
			parent.setIconImage(image);
		}catch (Exception e){System.out.println("The file could not be read!!!!");}
		
		JPanel fontPanel = new JPanel();
		fontName = new JComboBox(new String[] {"TimesRoman", "Helvetica", "Courier"});
		fontName.setSelectedIndex(1);
		fontName.addActionListener(this);
		fontSize = new JTextField("12",4);
		fontSize.setHorizontalAlignment(SwingConstants.RIGHT);
		fontSize.addActionListener(this);
		fontBold = new JCheckBox("Bold");
		fontBold.setSelected(true);
		fontBold.addActionListener(this);
		fontItalic=new JCheckBox("Italic");
		fontItalic.addActionListener(this);
		
		fontPanel.add(fontName);
		fontPanel.add(new JLabel(" Size: "));
		fontPanel.add(fontSize);
		fontPanel.add(fontBold);
		fontPanel.add(fontItalic);
		
		c.add(fontPanel, BorderLayout.NORTH);
		
		colorChooser = new JColorChooser(Color.black);
		colorChooser.getSelectionModel().addChangeListener(new ChangeListener(){
			public void stateChanged(ChangeEvent e){
				updatePreviewColor();
			}
		});
		c.add(colorChooser, BorderLayout.CENTER);
		
		JPanel previewPanel = new JPanel(new BorderLayout());
		previewLabel = new JLabel ("Here is a sample of this font.");
		previewLabel.setForeground(colorChooser.getColor());
		previewPanel.add(previewLabel,BorderLayout.CENTER);
		
		JButton okButton = new JButton("OK");
		okButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
				closeAndSave();
			}
		});
		JButton cancelButton = new JButton("Cancel");
		cancelButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
				closeAndCancel();
			}
		});
		
		JPanel controlPanel = new JPanel();
		controlPanel.add(okButton);
		controlPanel.add(cancelButton);
		previewPanel.add(controlPanel, BorderLayout.SOUTH);
		
		previewPanel.setMinimumSize(new Dimension(100,100));
		previewPanel.setPreferredSize(new Dimension(100,100));
		
		c.add(previewPanel, BorderLayout.SOUTH);
	}
	
	public void actionPerformed(ActionEvent ae){
		
		if(!StyleConstants.getFontFamily(attributes).equals(fontName.getSelectedItem())){
			StyleConstants.setFontFamily(attributes,(String)fontName.getSelectedItem());
		}
		
		if(StyleConstants.getFontSize(attributes)!= Integer.parseInt(fontSize.getText())){
			StyleConstants.setFontSize(attributes, Integer.parseInt(fontSize.getText()));
		}
		
		if(StyleConstants.isBold(attributes)!=fontBold.isSelected()){
			StyleConstants.setBold(attributes, fontBold.isSelected());			
		}
		
		if(StyleConstants.isItalic(attributes)!=fontItalic.isSelected()){
			StyleConstants.setItalic(attributes, fontItalic.isSelected());
		}
		
		updatePreviewFont();	
	}
	
	protected void updatePreviewFont(){
		String name = StyleConstants.getFontFamily(attributes);
		boolean bold = StyleConstants.isBold(attributes);
		boolean ital = StyleConstants.isItalic(attributes);
		int size = StyleConstants.getFontSize(attributes);
		
		Font f = new Font(name, (bold?Font.BOLD:0)+(ital?Font.ITALIC:0),size);
		previewLabel.setFont(f);		
	}
	
	protected void updatePreviewColor(){
		previewLabel.setForeground(colorChooser.getColor());
		previewLabel.repaint();
	}
	
	public Font getNewFont(){return newFont;}
	public Color getNewColor(){return newColor;}
	public AttributeSet getAttributes(){return attributes;}
	
	public void closeAndSave(){
		messageBox.setForeground(previewLabel.getForeground());
		messageBox.setFont(previewLabel.getFont());
		dispose();
		
	}
	
	public void closeAndCancel(){
		newFont=null;
		newColor=null;
		dispose();
	}
}
